package models

import (
	"github.com/go-xorm/xorm"
	"smartLearn/public/common"
)

type UserExam struct {
	Id           int `json:"id" xorm:"not null pk autoincr comment('主键') INT(11)"`
	UserId int `json:"user_id" xorm:"not null comment('用户id')  INT(11)"`
	SubjectId int `json:"subject_id" xorm:"not null comment('学科id')  INT(11)"`
	Name string `json:"name" xorm:" null comment('名字') "`
	Comment string `json:"comment" xorm:" null comment('说明') "`
	Status int `json:"status" xorm:"not null comment('状态')  INT(11)"`


}
var userexam ="user_exam"

func(ue *UserExam) GetRow() bool {
	has, err := mEngine.Get(ue)
	if err==nil &&  has  {
		return true
	}
	return false
}
func (ue *UserExam) Add(questionIdsArr []int) (int,error) {
	session := mEngine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		// if returned then will rodefer session.Close()llback automatically
		return 0, err
	}
	//var uid int64
	_, err := session.Insert(ue)


	for _,questionId:=range questionIdsArr {
		questionModel := SystemQuestion{}
		questionModel.Id = questionId
		has := questionModel.GetRowById(questionId)
		if !has {
			//如果阶段不存在，忽略
			continue
		}

		userexamQuestionModel := UserExamQuestion{ExamId: ue.Id, QuestionId: questionId}

		has, err := session.Get(&userexamQuestionModel)
		if err != nil {
			return 0, err
		}
		//如果试卷题目不存在，添加试卷题目
		if !has {
			_, err = session.Insert(&userexamQuestionModel)
			if err != nil {
				return 0, err
			}
		}

	}

	if err != nil {
		return 0, err
	}

	return ue.Id,session.Commit()
}
func (ue *UserExam) CheckName() bool {
	var ids []int
	mEngine.Table(userexam).Where("user_id=?",ue.UserId).
		Where("name=?",ue.Name).
		Where("subject_id=?",ue.SubjectId).Cols("id").Find(&ids)

	if  len(ids)>=1  {
		return true
	}
	return false


}

func  (ue *UserExam)List( paging *common.Paging)([]UserExam,error) {
	var userExams  []UserExam
	session:=ue.getListSession()
	var err error
	var u UserExam
	paging.Total,err = session.Count(&u)
	paging.GetPages()
	if err!=nil {
		return userExams,err
	}

	if paging.Total<1 {
		return userExams,err
	}
	session1:=ue.getListSession()
	err=session1.Limit(int(paging.PageSize),int(paging.StartNums)).Find(&userExams)

	return userExams,err
}


func  (ue *UserExam) getListSession() *xorm.Session{

	session:=mEngine.Table(userexam).
		Where(userexam+".subject_id = ?",ue.SubjectId).
		Where(userexam+".user_id =? ",ue.UserId)

	if ue.Status!=0{
		session=session.Where(userexam+".status =? ",ue.Status)
	}
	if ue.Name !="{}" && ue.Name !=""{
		session=session.Where(userexam+".name like ? ",ue.Name+"%")
	}
	if ue.Comment !="{}" && ue.Comment !=""{
		session=session.Where(userexam+".comment like ? ",ue.Comment+"%")
	}


	return session
}
func (ue *UserExam) DeleteById() error{

	session := mEngine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		// if returned then will rodefer session.Close()llback automatically
		return err
	}
	/*
		数据库设置了级连删除
	*/
	if _, err := mEngine.Where("id = ?", ue.Id).Delete(ue); err != nil {
		return err
	}


	return session.Commit()
}
func (ue *UserExam) Update() (int,error) {
	session := mEngine.NewSession()
	defer session.Close()
	// add Begin() before any action
	if err := session.Begin(); err != nil {
		// if returned then will rodefer session.Close()llback automatically
		return 0, err
	}
	//var uid int64
	if _, err := mEngine.Exec("update "+userexam+" set status=? , name= ?, comment=? where id = ? " ,ue.Status,ue.Name,ue.Comment ,ue.Id); err != nil {
		return ue.Id,err
	}
	return ue.Id,session.Commit()
}